<!DOCTYPE html>
<html>
<head>
    <title>系统树管理_列表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=10, user-scalable=yes">

    <link rel="stylesheet" href="/js/zTree_v3/css/demo.css" type="text/css">
    <link rel="stylesheet" href="/js/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="/js/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="/js/zTree_v3/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="/js/zTree_v3/js/jquery.ztree.excheck.js"></script>

    <script type="text/javascript" src="/js/zTree_v3/js/jquery.ztree.exedit.js"></script>

    <script type="text/javascript" src="/js/public.js"></script>

    <style type="text/css">
        .ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}

        .left {
            float: left;
            width: 300px;
            height: 600px;
        }
        .right {
            margin-left: 310px;
            height: 600px;
        }
    </style>

</head>
<body>


<div >
    <div class="left" width="100%">
        <button type="info" icon="shuffle" onclick="saveCheck()"> 保存选择 </button>
        <ul id="treeDemo" class="ztree"></ul>
    </div>
</div>
<script>
    <!--
    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            //onClick:onClick
        }
    };




    function getTime() {
        var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
        return (h+":"+m+":"+s+ " " +ms);
    }

    var roleid = PB.getPara('roleid');




    var zNodes =[
        { id:1, pId:0, name:"父节点1 - 展开", open:true},
        { id:11, pId:1, name:"父节点11 - 折叠"},
        { id:111, pId:11, name:"叶子节点111"},
        { id:112, pId:11, name:"叶子节点112"},
        { id:113, pId:11, name:"叶子节点113"},
        { id:114, pId:11, name:"叶子节点114"},
        { id:12, pId:1, name:"父节点12 - 折叠"},
        { id:121, pId:12, name:"叶子节点121"},
        { id:122, pId:12, name:"叶子节点122"},
        { id:123, pId:12, name:"叶子节点123"},
        { id:124, pId:12, name:"叶子节点124"},
        { id:13, pId:1, name:"父节点13 - 没有子节点", isParent:true},

    ];


    var nodes = [];
    function initRes(){
        $.ajax({
            type: 'POST',
            url: '/api/sys/sysres/listAll',
            async:false,
            cache:false,
            timeout:1000*10,
            success: function(response){
                //alert(response.length);
                for (var i=0, l=response.length; i<l; i++) {
                    var n = {id:response[i].resid, pId:response[i].presid, name:response[i].resname};
                    nodes.push(n);
                }
            },
            error: function(){
                alert("服务器繁忙，请稍候再试");
            },
            dataType: 'json'
        });
        //return nodes;
    }

    //所有已经选中的节点，存储在这里
    var oldCheck={};
    function initCheck(){
        $.ajax({
            type: 'POST',
            url: '/api/sys/sysroleres/queryByRole',
            async:true,
            cache:false,
            timeout:1000*10,
            success: function(response){
                //所有的选中的ID放入到map
                for (var i=0, l=response.length; i<l; i++) {
                    oldCheck["id"+response[i].resid]=1;
                }

                //循环所有节点，进行选中处理
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                var nodes2 = zTree.transformToArray(zTree.getNodes());
                //var nodes2 = zTree.getNodes();
                for (var i=0, l=nodes2.length; i<l; i++) {

                    if(oldCheck["id"+nodes2[i].id]){
                        //nodes2[i].checked=true;
                        zTree.checkNode(nodes2[i], true, false, false);
                    }
                }
            },
            data: {'roleid':roleid},
            error: function(){
                alert("服务器繁忙，请稍候再试");
            },
            dataType: 'json'
        });
        //return nodes;
    }

    $(document).ready(function(){
        initRes();
        setting.check.enable = true;
        $.fn.zTree.init($("#treeDemo"), setting,nodes);
        initCheck();
        //$("#selectAll").bind("click", selectAll);
    });

    //保存选择
    function saveCheck(){
        //获取所有选中的节点
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes2 = treeObj.getCheckedNodes(true);
        checkids = ";";
        for (var i=0, l=nodes2.length; i<l; i++) {
            checkids+=nodes2[i].id+";";
        }
        $.ajax({
            type: 'POST',
            url: '/api/sys/sysroleres/save2',
            async:true,
            cache:false,
            timeout:1000*20,
            success: function(response){
                if(response.success){
                    alert("保存成功");
                }else{
                    alert(response.msg);
                }
            },
            data: {'roleid':roleid,'checkids':checkids},
            error: function(){
                alert("服务器繁忙，请稍候再试");
            },
            dataType: 'json'
        });
        //return nodes;
    }

    //-->

</script>
</body>
</html>
